package com.kgc.sbt.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.kgc.sbt.api.TrackApi;
import com.kgc.sbt.service.OrderService;
import com.kgc.sbt.beans.TrackForm;
import com.kgc.sbt.config.annotation.RequestPermission;
import com.kgc.sbt.utils.RedisUtils;
import com.kgc.sbt.utils.RequestResult;
import com.kgc.sbt.utils.ResultBuildUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created on 2021/6/23 0023.
 * <p>
 * Author: px
 * <p>
 * Description: 足迹控制入口
 */
@RestController
@Api(tags = "7 足迹操作入口")
@Slf4j
@RequestPermission
public class TrackController {

    @Autowired
    private RedisUtils redisUtils;

    @Autowired
    private OrderService orderService;

    @Reference
    private TrackApi trackApi;

    /**
     * 查询足迹
     */
    @GetMapping("/queryTrack")
    @ApiOperation(value = "查看足迹",notes = "支持统一返回。。。")
    public RequestResult<List<TrackForm>> queryTrack(HttpServletRequest request){

        // 获取用户标识的token
        String token = request.getHeader("token");

        if(token == null){
            log.error("------ 用户标识token为空 ------");
            return ResultBuildUtil.fail("token_error_null", "用户标识token为空");
        }

        // 根据token获取用户id
        Integer userId = orderService.selectIdByToken(token);
        // 判断用户id是否合法
        if(userId == 0){
            log.error("------ token非法，无法识别用户 ------");
            return ResultBuildUtil.fail("token_error_illegal", "用户标识token非法");
        }

        List<TrackForm> tracks = trackApi.selectTrack(userId);

        if (tracks.isEmpty()){
            return ResultBuildUtil.fail("1005","该用户没有浏览任何商品");
        }

        return ResultBuildUtil.success(tracks);
    }


    /**
     * 添加足迹
     */
    @GetMapping("/addTrack")
    @ApiOperation(value = "添加足迹", notes = "支持统一返回")
    public RequestResult<String> addTrackById( HttpServletRequest request,@RequestParam Integer productId){
        // 获取用户标识的token
        String token = request.getHeader("token");

        if(token == null){
            log.error("------ 用户标识token为空 ------");
            return ResultBuildUtil.fail("token_error_null", "用户标识token为空");
        }
        // 根据token获取用户id
        Integer userId = orderService.selectIdByToken(token);
        // 判断用户id是否合法
        if(userId == 0){
            log.error("------ token非法，无法识别用户 ------");
            return ResultBuildUtil.fail("token_error_illegal", "用户标识token非法");
        }


        trackApi.addTrack(userId,productId);
        return ResultBuildUtil.success("足迹已添加");

    }

    /**
     *  删除足迹
     */
    @GetMapping("/delTrack")
    @ApiOperation(value = "删除足迹", notes = "支持统一返回")
    public RequestResult<String> delTrackById(HttpServletRequest request,@RequestParam Integer productId) {

        // 获取用户标识的token
        String token = request.getHeader("token");

        if(token == null){
            log.error("------ 用户标识token为空 ------");
            return ResultBuildUtil.fail("token_error_null", "用户标识token为空");
        }

        // 根据token获取用户id
        Integer userId = orderService.selectIdByToken(token);
        // 判断用户id是否合法
        if(userId == 0){
            log.error("------ token非法，无法识别用户 ------");
            return ResultBuildUtil.fail("token_error_illegal", "用户标识token非法");
        }

        if (trackApi.delTrack(userId,productId)) {
            return ResultBuildUtil.success("删除该足迹成功");
        }
        return ResultBuildUtil.fail("7001", "删除失败，不存在该条足迹..");

    }

    /**
     * 清空足迹
     */
    @GetMapping("/deleteTrack")
    @ApiOperation(value = "清空足迹", notes = "支持统一返回")
    public RequestResult<String> deleteTrack(HttpServletRequest request) {

        // 获取用户标识的token
        String token = request.getHeader("token");

        if(token == null){
            log.error("------ 用户标识token为空 ------");
            return ResultBuildUtil.fail("token_error_null", "用户标识token为空");
        }

        // 根据token获取用户id
        Integer userId = orderService.selectIdByToken(token);
        // 判断用户id是否合法
        if(userId == 0){
            log.error("------ token非法，无法识别用户 ------");
            return ResultBuildUtil.fail("token_error_illegal", "用户标识token非法");
        }

        if (trackApi.deleteTrack(userId)) {
            return ResultBuildUtil.success("清空足迹成功");
        }
        return ResultBuildUtil.fail("7001", "删除失败，不存在该条足迹..");


    }


}
